package com.cn.jinl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.jinl.api.IOrderBackInfoService;
import com.cn.jinl.api.IOrderService;
import com.cn.jinl.domain.OrderBackInfo;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/orderStatistics")
public class OrderStatisticsController {

    @Autowired
    private IOrderBackInfoService orderBackInfoService;

    @Autowired
    private IOrderService orderService;
    // 全类吸管产品产量动态数据
    @RequestMapping("/getAllTypesNumList/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String, Object>> getAllTypesNumList(
            @RequestBody QueryForm queryForm,
            @PathVariable(value = "frontToken", required = true) String frontToken)
            throws Exception {
        JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
        Map<String, Object> list  = orderService.getAllTypesNumList(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }
    // 全类吸管产品产量动态数据下载
    @ResponseBody
    @RequestMapping("/exportAllTypesNumList")
    public void exportAllTypesNumList(@RequestParam(value = "queryJson",required = true) String queryJson, HttpServletResponse response) throws Exception{
        QueryForm queryForm = new QueryForm();
        JSONObject jsonObject = JSON.parseObject(queryJson);
        queryForm.setYear(jsonObject.getString("year"));
        queryForm.setName(URLDecoder.decode(jsonObject.getString("name"), "UTF-8"));
        orderService.exportAllTypesNumList(queryForm,response);
    }


    // 全类吸管产品销售额动态数据
  @RequestMapping("/getAllTypesPriceList/{frontToken}")
  @ResponseBody
  public JsonResponse<Map<String, Object>> getAllTypesPriceList(
          @RequestBody QueryForm queryForm,
      @PathVariable(value = "frontToken", required = true) String frontToken)
      throws Exception {
        JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
        Map<String, Object> list  = orderService.getAllTypesPriceList(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }
    // 全类吸管产品销售额动态数据下载
    @ResponseBody
    @RequestMapping("/exportAllTypesPriceList")
    public void exportAllTypesPriceList(@RequestParam(value = "queryJson",required = true) String queryJson, HttpServletResponse response) throws Exception{
        QueryForm queryForm = new QueryForm();
        JSONObject jsonObject = JSON.parseObject(queryJson);
        queryForm.setYear(jsonObject.getString("year"));
        queryForm.setName(URLDecoder.decode(jsonObject.getString("name"), "UTF-8"));
        orderService.exportAllTypesPriceList(queryForm,response);
    }


    //订单及销售额排名
    @RequestMapping("/OrderAndSalesRanking/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String, Object>> OrderAndSalesRanking(
            @RequestBody  QueryForm queryForm,
            @PathVariable(value = "frontToken", required = true) String frontToken)
            throws Exception {
        JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
        Map<String, Object> list  = orderService.OrderAndSalesRanking(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }

    // 订单及销售额排名下载
    @ResponseBody
    @RequestMapping("/exportOrderAndSalesRanking")
    public void exportOrderAndSalesRanking(@RequestParam(value = "queryJson",required = true) String queryJson, HttpServletResponse response) throws Exception{
        QueryForm queryForm = new QueryForm();
        JSONObject jsonObject = JSON.parseObject(queryJson);
        queryForm.setStartTime(jsonObject.getString("startTime"));
        queryForm.setName(URLDecoder.decode(jsonObject.getString("name"), "UTF-8"));
        orderService.exportOrderAndSalesRanking(queryForm,response);
    }


    // 单客户年度交货数据
    @RequestMapping("/customerDelivery/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String, Object>> customerDelivery(
            @RequestBody QueryForm queryForm,
            @PathVariable(value = "frontToken", required = true) String frontToken)
            throws Exception {
        JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
        Map<String, Object> list  = orderService.customerDelivery(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }
    // 单客户年度交货数据下载
    @ResponseBody
    @RequestMapping("/exportCustomerDelivery")
    public void exportCustomerDelivery(@RequestParam(value = "queryJson",required = true) String queryJson, HttpServletResponse response) throws Exception{
        QueryForm queryForm = new QueryForm();
        JSONObject jsonObject = JSON.parseObject(queryJson);
        queryForm.setYear(jsonObject.getString("year"));
        queryForm.setName(URLDecoder.decode(jsonObject.getString("name"), "UTF-8"));
        queryForm.setCustomer(URLDecoder.decode(jsonObject.getString("customer"), "UTF-8"));
        orderService.exportCustomerDelivery(queryForm,response);
    }



    // 单客户年度销售额数据
    @RequestMapping("/customerOrder/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String, Object>> customerOrder(
            @RequestBody QueryForm queryForm,
            @PathVariable(value = "frontToken", required = true) String frontToken)
            throws Exception {
        JsonResponse<Map<String, Object>> response = new JsonResponse<Map<String, Object>>();
        Map<String, Object> list  = orderService.customerOrder(queryForm);
        response.setResult(list);
        response.setSuccess(true);
        return  response;
    }

    // 单客户年度销售额数据下载
    @ResponseBody
    @RequestMapping("/exportCustomerOrder")
    public void exportCustomerOrder(@RequestParam(value = "queryJson",required = true) String queryJson, HttpServletResponse response) throws Exception{
        QueryForm queryForm = new QueryForm();
        JSONObject jsonObject = JSON.parseObject(queryJson);
        queryForm.setYear(jsonObject.getString("year"));
        queryForm.setName(URLDecoder.decode(jsonObject.getString("name"), "UTF-8"));
        queryForm.setCustomer(URLDecoder.decode(jsonObject.getString("customer"), "UTF-8"));
        orderService.exportCustomerOrder(queryForm,response);
    }
}
